package com.hyd.fragment;

import java.util.ArrayList;


import com.hyd.adapter.ActionItemAdapter;
import com.hyd.project.CompanyDetailActivity;
import com.hyd.project.R;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ActionFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
	private SwipeRefreshLayout refreshLayout;
	private ListView lv_action;
	private ArrayList<String> dataList = new ArrayList<String>();
	private ActionItemAdapter adapter;
	private Handler hander = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				refreshLayout.setRefreshing(false);
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.action_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		InitView();
		InitData();
		InitClick();
	}

	private void InitView(){
		refreshLayout = (SwipeRefreshLayout)getActivity().findViewById(R.id.action_refreshLayout);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark, android.R.color.holo_green_light,
				android.R.color.holo_orange_light, android.R.color.holo_red_light);
		lv_action = (ListView)getActivity().findViewById(R.id.action_lv);
	}
	
	private void InitData(){
		dataList.clear();
		for (int i = 0; i < 10; i++) {
			dataList.add(i + "");
		}
		adapter = new ActionItemAdapter(getActivity(), dataList);
		lv_action.setAdapter(adapter);
	}
	
	private void InitClick(){
		lv_action.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),CompanyDetailActivity.class);
				getActivity().startActivity(intent);
				getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}
		});
	}
	
	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		hander.sendEmptyMessageDelayed(1, 2000);  
	}
}
